package com.frxuexi.park2302.park

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.SearchView.OnQueryTextListener
import androidx.activity.viewModels
import androidx.core.view.GravityCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.paging.PagingData
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.park2302.R
import com.frxuexi.park2302.databinding.ActivityParkBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/ParkActivity")
@AndroidEntryPoint
class ParkActivity : BaseActivity<ActivityParkBinding, ParkIntent, ParkViewModel>() {
    override val binding: ActivityParkBinding by lazy { ActivityParkBinding.inflate(layoutInflater) }
    override val vm: ParkViewModel by viewModels<ParkViewModel>()
//    val parkAdapter by lazy { ParkAdapter() }
    val parkAdapter2 by lazy { ParkAdapter2() }
    var type="区域"
    var para=""

    override fun initData() {
        lifecycleScope.launch {
//            lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
                vm.uiState.collect {
                    when (it) {
                        is UiState.onSuccess<*> -> {
                            when(it.type){
                                RequestType.DELETE->{
                                    ToastUtils.showLong("删除成功")
                                    vm.sendIntent(ParkIntent.getParkByType("标准车位"))
                                }
                                RequestType.DEFAULT->{
                                    parkAdapter2.data.clear()
                                    val items = it.data as Collection<ParkBeanItem>
                                    parkAdapter2.addData(items)
//                                    val data = it.data as PagingData<ParkBeanItem>
//                                    parkAdapter.submitData(lifecycle,data)
                                }
                                RequestType.PARKITEM->{
                                    parkAdapter2.data.clear()
                                    val items = it.data as ParkBeanItem
                                    parkAdapter2.addData(items)
                                }

                                else -> {}
                            }

                        }


                        else -> {}
                    }
                }
//            }
        }

    }

    override fun initView() {
        binding.headPark.apply {
            headBack.setOnClickListener {
                finish()
            }
            headTitle.setText("车位管理")
            headAdd.setOnClickListener {
                ARouter.getInstance().build("/app/ParkAddActivity").navigation()
            }
        }

        binding.parkRv.apply {
            adapter = parkAdapter2
            layoutManager = LinearLayoutManager(this@ParkActivity)
        }

        parkAdapter2.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/ParkEditActivity").withString("pid",parkAdapter2.data.get(position).id.toString()).navigation()
        }

        /*parkAdapter.setOnItemLongClickListener(object :OnItemLongClickListener{
            override fun onItemLongClick(position: Int): Boolean {
                vm.sendIntent(ParkIntent.deletePark(parkAdapter.snapshot().get(position)?.id!!))
                return true
            }

        })

        parkAdapter.setOnItemClickListener(object :OnItemClickListener{
            override fun onItemClick(position: Int) {
                ARouter.getInstance().build("/app/ParkVRActivity").navigation()
            }

        })*/

//        val touch = ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(ItemTouchHelper.LEFT, ItemTouchHelper.LEFT) {
//            override fun onMove(
//                recyclerView: RecyclerView,
//                viewHolder: RecyclerView.ViewHolder,
//                target: RecyclerView.ViewHolder
//            ): Boolean {
//                return false
//            }
//
//            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
//                val position = viewHolder.adapterPosition
//                vm.sendIntent(ParkIntent.deletePark(parkAdapter.snapshot().get(position)?.id!!))
//            }
//
//        })
//
//         touch.attachToRecyclerView(binding.parkRv)


        binding.parkFilter.setOnClickListener {
            binding.parkDrawer.openDrawer(GravityCompat.END)
        }

        binding.parkSv.setOnQueryTextListener(object : OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                para=query!!
                if(type=="区域") {
                    vm.sendIntent(ParkIntent.getParkByArea(query!!))
                }else{
                    vm.sendIntent(ParkIntent.getParkByCarNo(query!!))
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if(newText.isNullOrEmpty()){
                    vm.sendIntent(ParkIntent.getParkByType("标准车位"))
                }
                return true
            }

        })

        binding.parkRgstatus.setOnCheckedChangeListener { group, checkedId ->
            var status=""
            when(checkedId){
                R.id.rb_statuskx->{
                    status="空闲"
                    binding.rbStatuskx.isChecked=false
                }
                R.id.rb_stauszy->{
                    status="已占用"
                    binding.rbStauszy.isChecked=false
                }
                R.id.rb_stauswh->{
                    status="维护中"
                    binding.rbStauswh.isChecked=false
                }
            }
            vm.sendIntent(ParkIntent.getParkByStatus(status))
            binding.parkDrawer.closeDrawer(GravityCompat.END)
        }

        binding.parkRgtype.setOnCheckedChangeListener { group, checkedId ->
            var type=""
            when(checkedId){
                R.id.rb_typea->{
                    type="标准车位"
                    binding.rbTypea.isChecked=false
                }
                R.id.rb_typeb->{
                    type="VIP车位"
                    binding.rbTypeb.isChecked=false
                }
            }
            vm.sendIntent(ParkIntent.getParkByType(type))
            binding.parkDrawer.closeDrawer(GravityCompat.END)
        }

        binding.parkCarNo.setOnClickListener {
            type="车牌号"
            vm.sendIntent(ParkIntent.getParkByCarNo(para))
            binding.parkDrawer.closeDrawer(GravityCompat.END)
        }
    }

    override fun onResume() {
        super.onResume()
        vm.sendIntent(ParkIntent.getParkByType("标准车位"))
    }

}